package com.yuyou.tas.job.service.Async;


import cn.hutool.core.exceptions.ExceptionUtil;
import com.yuyou.tas.service.api.admin.entity.CommJob;
import com.yuyou.tas.service.api.admin.service.CommJobAPI;
import com.yuyou.tas.service.api.feedback.service.FeedbackAPI;
import com.yuyou.tas.service.api.lesson.entity.LessonResource;
import com.yuyou.tas.service.api.lesson.service.LessonAPI;
import com.yuyou.tas.service.api.lesson.service.LessonResourceAPI;
import com.yuyou.tas.service.api.lesson.service.OfficeAPI;
import com.yuyou.tas.util.enums.admin.CommJobEnums;
import com.yuyou.tas.util.tool.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * Create by chengsiyuan on 2018/6/14-11:17
 **/
@Component
public class AsyncService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OfficeAPI officeAPI;

    @Autowired
    private LessonResourceAPI lessonResourceAPI;
    
    @Autowired
    private LessonAPI lessonAPI;

    @Autowired
    private FeedbackAPI feedbackAPI;

    @Autowired
    private CommJobAPI commJobAPI;

    @Async("taskExecutor")
    public void AsyncDeleteJob(Long id,Long commId) throws Exception {
        LessonResource lr=lessonResourceAPI.selectById(id);
        if(lr == null){
            commJobAPI.updateJob(commId , CommJobEnums.status.fail.getValue() , "lessonResource查询为空");
	        LogUtil.error(logger,Thread.currentThread().getName()+"-删除FileMetas失败,查询lessonSource-"+id+"");
        }else{
            try {
                officeAPI.deleteFileMetas(lr.getResourceId());
                LogUtil.info(logger,Thread.currentThread().getName()+"-删除FileMetas："+lr.getResourceId()+"-LessonResource:"+id);
            } catch (Exception e) {
                LogUtil.error(logger , "删除Office 文件失败：   "+ExceptionUtil.getMessage(e));
                commJobAPI.updateJob(commId , CommJobEnums.status.fail.getValue() , ExceptionUtil.getMessage(e));
            }
        }
    }

    @Async("feedbackTaskExecutor")
    public void AsynUploadJob(CommJob job){
        try {
			feedbackAPI.AsynUploadJob(job);
		} catch (Exception e) {
			commJobAPI.updateJob(job.getId(), CommJobEnums.status.fail.getValue(),e.getMessage());
		}
        LogUtil.info(logger , Thread.currentThread().getName()+"-处理微信图片音频，job ID："+job.getId());
    }
    

   
    
}
